package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.DepositRecordDetailModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.MyAlertDialog;

public class DepositRecordDetailAct extends BaseActivity implements OnClickListener {
    private ImageView ivBack;
    private RelativeLayout rlLocate;
    private ImageView mStoreImage;
    private LinearLayout mDepositDetailLayout;
    private TextView mShopName, mShopAddress, mOrderNo, mDeposoitor, mContactPhone, mFusedGrams, mMeltGrams,
            mReseverTime;
    private ImageView ivCall;
    private Button btnCancle;
    private DepositRecordDetailModel model;
    private final int GET_DEPOSIT_RECORD_DETAIL_SUCCESS = 1;
    private Handler handle = new Handler() {

        @SuppressWarnings("deprecation")
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case GET_DEPOSIT_RECORD_DETAIL_SUCCESS:
                    model = (DepositRecordDetailModel) msg.obj;
                    Glide.with(DepositRecordDetailAct.this).load(model.getShopImage().trim()).placeholder(R.drawable.placeholder_shop_big).error(R.drawable.placeholder_shop_big).into(mStoreImage);
                    mShopName.setText(model.getShopName());
                    mShopAddress.setText(model.getShopAddress());
                    mOrderNo.setText(model.getOrderNo());
                    String cancelStatus = model.getOrderStatus();
                    if (cancelStatus.equals("0")) {
                        btnCancle.setText(model.getStatusText());
                    } else {
                        btnCancle.setText(model.getStatusText());
                        btnCancle.setBackgroundColor(getResources().getColor(R.color.color_btn_gray));
                        btnCancle.setEnabled(false);
                    }
                    mDeposoitor.setText(model.getContact());
                    mContactPhone.setText(model.getTelephone());
                    if (!"".equals(model.getAfterMeltedQuantity())) {
                        mDepositDetailLayout.setVisibility(View.VISIBLE);
                        mFusedGrams.setText(model.getBeforeMeltedQuantity());
                        mMeltGrams.setText(model.getAfterMeltedQuantity());
                    }
                    mReseverTime.setText(model.getReserveTime());
                    break;

                default:
                    break;
            }
        }

    };
    private String orderNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit_record_detail);
        tintManager.setStatusBarAlpha(0f);
        initView();
        initdata();
    }

    private void initdata() {
        orderNo = getIntent().getStringExtra("order_no");
        ActionUtil actionUtil = new ActionUtil(DepositRecordDetailAct.this);
        actionUtil.getDepositRecordDetail(orderNo);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                DepositRecordDetailModel depositModel = (DepositRecordDetailModel) model;
                if (depositModel == null) {
                    UIHelper.showToast(DepositRecordDetailAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (depositModel.getErrorMsg() != null) {
                    if (depositModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(DepositRecordDetailAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(DepositRecordDetailAct.this, depositModel.getErrorMsg());
                    }

                } else {
                    Message msg = new Message();
                    msg.what = GET_DEPOSIT_RECORD_DETAIL_SUCCESS;
                    msg.obj = depositModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_left_arrow);
        rlLocate = (RelativeLayout) findViewById(R.id.locate_layout);
        mStoreImage = (ImageView) findViewById(R.id.shop_image);
        ivCall = (ImageView) findViewById(R.id.iv_call);
        mDepositDetailLayout = (LinearLayout) findViewById(R.id.deposit_detail_layout);
        mShopName = (TextView) findViewById(R.id.shop_name);
        mShopAddress = (TextView) findViewById(R.id.shop_address);
        mOrderNo = (TextView) findViewById(R.id.deposit_order_no);
        mDeposoitor = (TextView) findViewById(R.id.deposit_account);
        mContactPhone = (TextView) findViewById(R.id.contact_phone);
        mFusedGrams = (TextView) findViewById(R.id.fused_gold_grams);
        mMeltGrams = (TextView) findViewById(R.id.melt_grams);
        mReseverTime = (TextView) findViewById(R.id.resever_time);
        btnCancle = (Button) findViewById(R.id.btn_cancle_deposit);
        ivBack.setOnClickListener(this);
        rlLocate.setOnClickListener(this);
        ivCall.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left_arrow:
                finish();
                break;
            case R.id.locate_layout:
                if (model != null) {
                    ActivityUtils.gotoShopPosition(DepositRecordDetailAct.this, model.getShopAddress(), model.getX(),
                            model.getY(), model.getShopName());
                }
                break;
            case R.id.iv_call:
                if (model != null) {
                    UIHelper.showCallRemindDialog(DepositRecordDetailAct.this, model.getShopPhone());
                }
                break;

            case R.id.btn_cancle_deposit:
                new MyAlertDialog(DepositRecordDetailAct.this).builder().setTitle("是否取消预约？")
                        .setPositiveButton("立即取消", true, new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                cancleDepositOrder();
                            }
                        }).setNegativeButton("暂不取消", new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    }
                }).show();

                break;

            default:
                break;
        }
    }

    private void cancleDepositOrder() {
        ActionUtil actionUtil = new ActionUtil(DepositRecordDetailAct.this);
        actionUtil.cancleDepositOrder(orderNo);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                if (model == null) {
                    UIHelper.showToast(DepositRecordDetailAct.this, getString(R.string.rank_fail));
                    return;
                }
                if (model.getErrorMsg() != null) {
                    if (model.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(DepositRecordDetailAct.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(DepositRecordDetailAct.this, model.getErrorMsg());
                    }

                } else {
                    UIHelper.showToast(DepositRecordDetailAct.this, model.getInfo());
                    btnCancle.setBackgroundColor(getResources().getColor(R.color.color_btn_gray));
                    btnCancle.setText("已取消");
                    btnCancle.setEnabled(false);
                }
            }
        });
    }

}
